import { Body, Controller, Get, HttpException, HttpStatus, Param, ParseIntPipe, Post, Query, UseFilters } from "@nestjs/common";
import { ApiBody, ApiQuery, ApiResponse, ApiTags } from "@nestjs/swagger";
import { HttpExceptionFilter } from "src/common/filters/http-exception.filter";
import { Result } from "src/model/result";
import { Test } from "src/model/test.model";
import { User } from "src/model/user.model";
import { UserService } from "src/service/user.service";

@ApiTags("users")
// @UseFilters(new HttpExceptionFilter())
@Controller("users")
export class UserController {
    constructor(private readonly userService: UserService) {
    }

    @Get("/list")
    async list(): Promise<Result<any>> {
        return await this.userService.list();
    }

    @Get("/get")
    @ApiQuery({ name: 'id' })
    getUser(@Query("id") id: string): string {
        return "hello" + id
    }

    @Post()
    @ApiResponse({ status: 201, description: 'The record has been successfully created.' })
    @ApiBody({ type: User })
    async saveUser(@Body() user: User): Promise<Result<any>> {
        return await this.userService.save(user);
    }
    @Post("/test")
    testResult(@Body() result: Test): void {
        console.log(result)
    }

    @Get("/testHot")
    testHot(): string {
        return "testHOt"
    }
    @Get("exception")
    async testException() {
        throw new HttpException({
            status: HttpStatus.FORBIDDEN,
            error: 'This is a custom message',
        }, HttpStatus.FORBIDDEN);
    }

    @Get("/byId/:id")
    getById(@Param("id", new ParseIntPipe()) id: string): string {
        console.log(typeof id)
        return id
    }

}

